<?php
require_once($application_folder."/controllers/navigator.php");
class Media extends Navigator{

    function __construct(){
        parent::__construct();
        
        /************************************************************
        * CHECK LOGIN
        *************************************************************/
        if(!$this->session->userdata('logged_in')){
            redirect(base_url());
        }
        
        $this->load->model('articlem','art');
        $this->load->helper('date');
    }

    function index(){
        $this->preview();
    }

    function preview(){

        /************************************************************
        * GET MEDIA TABLE
        *************************************************************/


        switch ($this->session->userdata('typeofuser_id')){

            case 'JN':   $this->db->where('user_id', $this->session->userdata('id'));  break;
            case 'MP':   $this->db->where('user_id', $this->session->userdata('id'));  break;              

        }     

        $this->db->where($array = array('croped !=' => TRUE));
        $data['media'] = $this->db->get('media')->result_array();

        
        $this->db->where($array = array('parent !=' => 0));  
        $data['directoryMedia'] = $this->db->get('directory')->result_array();

        $this->core_cms('media', $data, 'preview');                                  
    }

    function newform(){

        /************************************************************
        * GET MEDIA TABLE
        *************************************************************/
        switch ($this->session->userdata('typeofuser_id')){

            case 'JN':   $this->db->where('user_id', $this->session->userdata('id'));  break;
            case 'MP':   $this->db->where('user_id', $this->session->userdata('id'));  break;              

        }

        $data['media'] = $this->db->get('media')->result_array();

        /************************************************************
        * GET DIRECTORY TABLE
        *************************************************************/
        $this->db->where($array = array('parent !=' => 0));  
        $data['directoryMedia'] = $this->db->get('directory')->result_array();

        /*if(isset($_GET['id'])){
        $data['columnname'] = $this->db->get_where('subcolumn', array('id' => $_GET['subcolumnid']))->result_array();    
        }   */

        switch($_GET['type']){

            case 'img': $this->core_cms('media', $data, 'new_edit_form_img'); break;
            case 'yt': $this->core_cms('media', $data, 'new_edit_form_yt'); break;

        }
    }

    function editform(){

        /************************************************************
        * GET MEDIA TABLE DATA
        *************************************************************/         
        $this->db->where('id',$_GET['id']);

        switch ($this->session->userdata('typeofuser_id')){

            case 'JN':   $this->db->where('user_id', $this->session->userdata('id'));  break;
            case 'MP':   $this->db->where('user_id', $this->session->userdata('id'));  break;              

        }

        $data['mediadata'] = $this->db->get('media')->result_array();

        /************************************************************
        * GET DIRECTORY TABLE
        *************************************************************/
        $this->db->where($array = array('parent !=' => 0));  
        $data['directoryMedia'] = $this->db->get('directory')->result_array();

        switch($_GET['type']){

            case 'img': $this->core_cms('media', $data, 'new_edit_form_img'); break;
            case 'yt': $this->core_cms('media', $data, 'new_edit_form_yt'); break;

        }


    }

    function edit_insert(){

        switch ($_POST['appmode']){

            case 'insert':
                $this->db->insert('media', array(
                'type' => $_POST['type'], 
                'name' => $_POST['name'], 
                'description' => $_POST['description'],
                'author' => $_POST['author'],
                'created' => time(),
                'tags' => $_POST['tags'],
                'file' => $_POST['filename'],
                'directory_id' => $_POST['parent'],
                'user_id' => $this->session->userdata('id'),
                'croped' => FALSE
                ));
                echo json_encode(array('action'=> true)); 
                break;

            case 'edit':

                $this->db->where('id', $_POST['id']);

                $myArray = array(
                'type' => $_POST['type'], 
                'name' => $_POST['name'], 
                'description' => $_POST['description'],
                'author' => $_POST['author'],
                'modified' => time(),
                'tags' => $_POST['tags'],
                'file' => $_POST['filename'],
                'directory_id' => $_POST['parent'],
                'croped' => FALSE
                );

                $this->db->update('media', $myArray);


                echo json_encode(array('action'=> true));

                break;

        }

        if($_POST['type']=='yt'){

            //$this->load->library('curl');

            $image_url = "http://img.youtube.com/vi/".$_POST['filename']."/0.jpg";
            $ch = curl_init();
            $timeout = 0;
            curl_setopt ($ch, CURLOPT_URL, $image_url);
            curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

            // Getting binary data
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);

            $image = curl_exec($ch);
            curl_close($ch);

            $f = fopen('./tempatt/'.$_POST['filename'].'.jpg', 'w');
            //$f = fopen('./admin/tempatt/'.$_POST['filename'].'.jpg', 'w');
            fwrite($f, $image);
            fclose($f);

        }

    }

    function deleteone(){      

        $t = $this->db->count_all_results('media');

        $this->db->delete('video_counter',array('media_id' => $_POST['id']));
        
        $this->db->where('id',$_POST['id']);
        $this->db->delete('media');

        if($t == $this->db->count_all_results('media')){
            echo json_encode(array('action'=> false, 'msg' => 'Medija se koristi u nekom od članaka. Ne možete obrisati'));
        }else{
            echo json_encode(array('action'=> true));
        }


    }

    function ddd(){

        $t = $this->db->count_all_results('media');

        $this->db->where('id',172);
        $this->db->delete('media');

        if($t == $this->db->count_all_results('media')){
            echo json_encode(array('action'=> false, 'msg' => 'Medija se koristi u nekom od članaka. Ne možete obrisati'));
        }else{
            echo json_encode(array('action'=> true));
        }

    }

    function getMediaByFolder($folderid=NULL){

        if($folderid==NULL){
            $folderid = $_POST['id'];
        }

        $this->db->where('directory_id',$folderid);

        switch ($this->session->userdata('typeofuser_id')){

            case 'JN':   $this->db->where('user_id', $this->session->userdata('id'));  break;
            case 'MP':   $this->db->where('user_id', $this->session->userdata('id'));  break;              

        }

        
        $this->db->where($array = array('croped !=' => TRUE));
        $r = $this->db->get('media')->result_array();
        $data['media'] = $r;

        /************************************************************
        * GET DIRECTORY TABLE
        *************************************************************/
        $this->db->where($array = array('parent !=' => 0));  
        $data['directoryMedia'] = $this->db->get('directory')->result_array();



        switch($_POST['who']){

            case 'media':
                $html = $this->load->view('cms/media/getMediaByFolderMedia', $data, TRUE); 
                break;

            case 'article':
                $html = $this->load->view('cms/media/getMediaByFolderArticle', $data, TRUE); 
                break;
        }


        if(count($r)==0){
            echo json_encode(array('action'=> false, 'msg' => 'Nema medija u ovom direktorijumu.'));
        }else{
            echo json_encode(array('action'=> true, 'msg' => '', 'html' => $html)); 
        }



    }

}
